package com.cloudeye.business.base.util;


import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 日期帮助类
 * @author jianglb
 * @date 2021/4/9 15:17
 */
public class DateUtil implements Serializable {
    private static final long serialVersionUID = 2467154164296626748L;
    public static final String DATE_FORMAT_YYYY = "yyyy";
    public static final String DATE_FORMAT_YYYYMM = "yyyyMM";
    public static final String DATE_FORMAT_YYYY_MM = "yyyy-MM";
    public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
    public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT_YYYY_MM_DD_HH_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS = "yyyyMMddHHmmssSSS";
    public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISS = "yyyyMMddHHmmss";
    public static final DateTimeFormatter DATE_FORMATTER_YYYYMMDD = DateTimeFormatter.ofPattern("yyyyMMdd");

    public DateUtil() {
    }

    public static LocalDateTime dateConveterLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    public static int getYear(Date date) {
        LocalDateTime localDateTime = dateConveterLocalDateTime(date);
        return localDateTime.getYear();
    }

    public static int getMonth(Date date) {
        LocalDateTime localDateTime = dateConveterLocalDateTime(date);
        return localDateTime.getMonthValue();
    }

    public static int getDayOfYear(Date date) {
        LocalDateTime localDateTime = dateConveterLocalDateTime(date);
        return localDateTime.getDayOfYear();
    }

    public static int getHourOfDay() {
        return LocalDateTime.now().getHour();
    }

    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static String parseDateToString(Date date, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    public static String parseDateToString(Date date, String format, Locale locale) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, locale);
        return dateFormat.format(date);
    }

    public static String parseTimestampToString(Timestamp timestamp, String format) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(timestamp);
    }

    public static Date parseStringToDate(String date, String formatPattern) {
        if (!StringUtils.hasLength(date)) {
            return null;
        } else {
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat(formatPattern);
                return dateFormat.parse(date);
            } catch (ParseException var3) {
                var3.printStackTrace();
                return null;
            }
        }
    }

    public static Date parse(String dateText, String formatPattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatPattern);
        LocalDateTime localDateTime = LocalDateTime.parse(dateText, dateTimeFormatter);
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date parse(String dateText) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime localDateTime = LocalDateTime.parse(dateText, dateTimeFormatter);
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }


    /**
     * 得到指定时间在指定分钟数之后的时间(传入负整数表示之前)
     *
     * @param date
     * @param afterMinute
     * @return
     */
    public static Date getDateAfterMinute(Date date, int afterMinute) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, afterMinute);
        return calendar.getTime();
    }

    /**
     * 得到指定时间在指定天数之后的时间(传入负整数表示之前)
     *
     * @param date
     * @param afterDay
     * @return
     */
    public static Date getDateAfter(Date date, int afterDay) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, afterDay);
        return calendar.getTime();
    }



}